<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
  <head>
    <title>教材吧-我的购物车</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<%@ include file="/base/jsp/includeweb.jsp"%>
    <link type="text/css" rel="stylesheet" href="${path}/base/css/order.css"/>
  </head>
  <body>
  <%@ include file="/base/jsp/header.jsp"%>
  <%@ include file="/base/jsp/search-header.jsp"%>
  <div id="content-wrapper" class="content-wrapper border-top-org clear">
    <h2>我的购物车</h2>
    <div id="check-all">
         <input id="cart-check-all" name="cart-check-all" type="checkbox" checked="true"/>全选
    </div>
    
    <div id="cart-info-wrapper">
        
    </div>
    
    <div id="cart-price-total">
                     商品总计：<span id="price-total"></span>元
    </div>
    
     <div id="cart-buy">
         <input id="buy-but" type="button" class="but-org" value="去结账"/>
    </div>
    
    
  </div>
  <%@ include file="/base/jsp/footer.jsp"%>
  </body>
  <script type="text/javascript">
   var username = "${sessionScope.account}";
   var userid = "${sessionScope.userid}";
   var nump = /^\d+$/;
   var total = 0;
   getCarts();
   $(function(){
     
     $("#buy-but").click(function(){
         if(total <= 0.0){
             alert("请选择一本书");
             return;
         }
         var array = new Array();
         $("input[name=cart-check]:checked").each(function(){
               var cart = $(this).parents(".cart");
               var bid = cart.attr("bookid");
               //var count = cart.find("#number").val();
               array.push(bid);
         });
         var bookids = array.join(",");
         var query = encodeURI("userid="+userid+"&bookid="+bookids);
         location.href="webaddorderall.do?"+query;
     })
     
     
     
     $("#cart-check-all").click(function(){
         if($(this).attr("checked")){
             $("input[name=cart-check]").each(function(){
               $(this).attr("checked",true);
             })
         }else{
             $("input[name=cart-check]").each(function(){
               $(this).attr("checked",false);
             })
         }
         calTotal();
     })
     
  
  })
  
  function calTotal(){
      total = 0;
      $("input[name=cart-check]:checked").each(function(){
            var par = $(this).parents(".cart");
            //alert(par.find(".number").val());
            total += par.find("#number").val() * par.find(".price span").text();
      })
      $("#price-total").text(total);
  }
  
  function getCarts(){
     $(".cart").remove();
     request("listCart.do","userid="+userid,function(data){
         var d = data.data;
         total = 0;
         for(var i in d){
             var cart = d[i];
             var row = $('<div class="cart clear" id=""><div class="check-info"><input type="checkbox" class="cart-check" name="cart-check"/></div>'
             +'<div class="img-info"><a target="_blank"><img/></a></div>'
             +'<div class="name-info"><p class="name"><a target="_blank"></a></p></div>'
             +'<div class="price-info"><p class="price"><span></span>元</p></div><div class="num-info"><div class="num-wrapper clear">'
             +'<img id="buy-num-minus" class="buy-num-minus" src="${path}/base/img/minus-but.png"/><input id="number" class="number" name="number" type="text" value="1"/>'
             +'<img id="buy-num-plus" class="buy-num-plus" src="${path}/base/img/plus-but.png"/></div></div>'
             +'<div class="delete-info"><input class="del-but" type="button" value="删除"></div></div>');
             row.attr("bookid",cart.bookid);
             row.attr("userid",cart.userid);
             row.find(".img-info a").attr("href","bookdetail.do?id="+cart.bookid);
             row.find(".img-info a img").attr("src",getImg(cart.bookImg));
             row.find(".name-info a").attr("href","bookdetail.do?id="+cart.bookid);
             row.find(".name-info a").text(cart.bookName);
             row.find("#number").val(cart.count);
             row.find(".cart-check").attr("checked","true");
             row.find(".price-info .price span").text(cart.price);
             total += cart.price * cart.count;
             $("#cart-info-wrapper").append(row);
         }
         $("#price-total").text(total);
         $(".buy-num-minus").click(function(){
             var number = $(this).parents(".cart").find("#number");
             var num = parseInt(number.val());
             if(nump.test(num)==false){
                 alert("请输入正确的数字格式");
                 return;
             }
             if(num > 0){
                number.val(num-1);
                calTotal();
             }
             var bookid = $(this).parents(".cart").attr("bookid");
             var userid = $(this).parents(".cart").attr("userid");
             var count =  number.val();
             request("saveCart.do","userid="+userid+"&bookid="+bookid+"&count="+count,function(data){
             });
         });
         $(".buy-num-plus").click(function(){
             var number = $(this).parents(".cart").find("#number");
             var num = parseInt(number.val());
             if(nump.test(num)==false){
                 alert("请输入正确的数字格式");
                 return;
             }
             number.val(num+1);
             calTotal();
             var bookid = $(this).parents(".cart").attr("bookid");
             var userid = $(this).parents(".cart").attr("userid");
             var count =  number.val();
             request("saveCart.do","userid="+userid+"&bookid="+bookid+"&count="+count,function(data){
             });
         });
         $(".number").focusout(function(){
             var number = $(this).parents(".cart").find("#number");
             var num = parseInt(number.val());
             if(nump.test(num)==false){
                 alert("请输入正确的数字格式");
                 return;
             }
             calTotal();
             var bookid = $(this).parents(".cart").attr("bookid");
             var userid = $(this).parents(".cart").attr("userid");
             var count =  number.val();
             request("saveCart.do","userid="+userid+"&bookid="+bookid+"&count="+count,function(data){
             });
         });
         $("input[name=cart-check]").click(function(){
           calTotal();
         });
         $(".del-but").click(function(){
             if(confirm("确定要删除么?")){
                 var bookid = $(this).parents(".cart").attr("bookid");
                 var uid = $(this).parents(".cart").attr("userid");
                 request("delCart.do","userid="+uid+"&bookid="+bookid,function(data){
                    if(data.success==true){
                       alert("删除成功");
                       getCarts();
                    }else{
                       alert("删除失败");
                    }
                 });
             }
         })
      })
  
  }
  
  
  
  </script>
</html>